/**
 * common function
 * @author Alvin 20130525
 * 
 */

function zoomScore() {
    var day;
    if (typeof($("input[name='img_zoom']").attr("checked")) == 'undefined') {
        day = 0;
    } else {
        day = 1;
    }
    countScore(day, 'zoom');
    countScoreSum();
}

function linkScore() {
    var day;
    if (typeof($("input[name='link_status']").attr("checked")) == 'undefined') {
        day = 0;
    } else {
        day = $("input[name='link_days']").val();
    }
    countScore(day, 'link')
    countScoreSum();
}

function pageScore() {
    var day;
    if (typeof($("input[name='page_status']").attr("checked")) == 'undefined') {
        day = 0;
    } else {
        day = $("input[name='page_days']").val();
    }
    countScore(day, 'page')
    countScoreSum();
}

function dayScore() {
    var day;
    if ($("input[name='days_status']:checked").val() == 0) {
        day = 1;
    } else if($("input[name='days_status']:checked").val() == 1){
        day = $("input[name='days']").val();
    }else {
        return false;
    }
    countScore(day, 'day_pay')
    countScoreSum();
}

function indextopScore() {
    var day;
    if (typeof($("input[name='index_top_status']").attr("checked")) == 'undefined') {
        day = 0;
    } else {
        day = $("input[name='index_top_days']").val();
    }
    countScore(day, 'index_top')
}

function cattopScore() {
    var day;
    if (typeof($("input[name='cat_top_status']").attr("checked")) == 'undefined') {
        day = 0;
    } else {
        day = $("input[name='cat_top_days']").val();
    }
    countScore(day, 'cat_top')
}

function countScore(day, type) {
    $.ajax({
        url: '/user/gezi/countScore.html',
        type: 'POST',
        dataType: 'json',
        data: {
            'day': day,
            'type': type
        },
        success: function(data) {
            if (data['status'] == true) {
                $("#tip_" + type).html(data['data']);
            } else {
                alert(data['msg']);
            }
        }
    });
}

function countScoreSum() {
    $.ajax({
        url: '/user/gezi/countScoreSum.html',
        type: 'POST',
        dataType: 'json',
        data: {
            'link': $("input[name='link_days']").val(),
            'link_status': $("input[name='link_status']").attr("checked"),
            'zoom': $("input[name='img_zoom']").attr("checked"),
            'page': $("input[name='page_days']").val(),
            'page_status': $("input[name='page_status']").attr("checked"),
            'days': $("input[name='days']").val(),
            'days_status': $("input[name='days_status']:checked").val(),
            //置顶
            'index_top': $("input[name='link_days']").val(),
            'cat_top': $("input[name='link_days']").val()
        },
        success: function(data) {
            if (data['status'] == true) {
                $("#tip_total").html(data['data']);
            } else {
                alert(data['msg']);
            }
        }
    });
}


function showmsg(msg){
    art.dialog({
        id: 'msgdialog',
        content: msg
    }).title('3秒后关闭').time(3);
}

function zoomimage(msg){
    var zoomimg = art.dialog({
        id: 'zoomimg',
        content: msg
    });
}

function pageEdit() {
    art.dialog.open('/user/gezi/linkPage.html', {
        id: 'pageEdit',
        width: '100%',
        height: '100%',
        left: '0%',
        top: '0%',
        fixed: true,
        resize: false,
        drag: false,
        title: '次级页面编辑',
        ok: function() {
            var iframe = this.iframe.contentWindow;
            if (!iframe.document.body) {
                alert('iframe还没加载完毕呢')
                return false;
            }
            ;
            var form = iframe.document.getElementById('dopage');
            var title = iframe.document.getElementById('title');
            var content = iframe.document.getElementById('content');
            $.ajax({
                url: '/user/article/create.html',
                type: 'POST',
                dataType: 'json',
                data: {
                    'title': title.value(),
                    'content': content.value()
                },
                success: function(data) {
                    if (data['status'] == true) {
                        $("#tip_total").html(data['data']);
                    } else {
                        alert(data['msg']);
                    }
                }
            });
            return false;
        },
        cancel: true
    });
}

var aID;
function getArticleID(url) {
    window.location.herf = url;
    $("#editConPage").html('(等待发布返回中......)');
    aID = setInterval(ajaxAID, 1000);
}

function ajaxAID() {
    $.ajax({
        url: '/user/article/articleID.html',
        type: 'POST',
        dataType: 'json',
        success: function(data) {
            if (data['status'] === true) {
                $("#editConPage").html('关联的文章标题：《' + data['data']['title'] + '》, 点击可以重新发布');
                $("input[name='page_id']").val(data['data']['aid']);
                clearInterval(aID);
            }
        }
    });
}

function catChild(){
    var pid = $("#cat_parentid").val();
    $.ajax({
        url: '/user/catad/childCat.html',
        type: 'POST',
        data: {
            pid: pid
        },
        dataType: 'json',
        success: function(data) {
            if (data['status'] == true) {
                var html = '';
                var i;
                for(i=0; data['data'].length > i; i++){
                    html += "<option value=\""+data['data'][i].id+"\">"+data['data'][i].name+"</option>";
                }
                $("#cat_id").html(html);
            }
        }
    });
}

//发送站内信
function sendMessage(uid){
    var sMsg = art.dialog({
        follow: document.getElementById('sendBtn'),
        content: '标题：<input name="title" value="" id="mtitle"></br></br>内容：<textarea rows="3" cols="20" id="mbox"></textarea>',
        padding: '20px',
        ok: function () {
            var title = $("#mtitle").val();
            var message = $("#mbox").val();
            $.ajax({
                type: "POST",
                url: '/home/ajax/sendMessage.html',
                data:   {
                    title: title,
                    message:message,
                    tuid:uid
                },
                success: function(data){
                    if (data['status'] == 'true') {
                        alert(data['msg']);
                    } else {
                        alert(data['msg']);
                    }
                    return true;
                } 
            });
        },
        okVal: '发送',
        cancel: true
    });
}

/**
 *  未读站内信
 */

function countMsg(){
    $.ajax({
        type: "POST",
        url: '/home/Ajax/countMessage.html',
        success: function(data){
            $("#newMsg").html('站内信('+data+')');
        } 
    });
}

/**
 *  读取站内信内容
 */
function readMsg(id){
    $.ajax({
        type: "POST",
        url: '/user/message/read.html',
        data: {
            id:id
        },
        success: function(data){
            var content;
            var msgDialog = art.dialog({
                id: 'message_box',
                title: '查看短信',
                lock: true,
                background: '#000', // 背景色
                opacity: 0.87,
                cancel: function(){
                    location.reload();
                }
            });
            if(data['status'] == true){
                msgDialog.content(data['data']);
            }else {
                msgDialog.content(data['msg'])
            }
            
        } 
    });
}

/**
 *  发表评论
 */

function commit(con_id, con_type){
    var title = $("#title").val();
    var content = $("#content").val();
    if(title == false){
        alert('评论标题不能为空');
    }
    if(content == false){
        alert("评论内容不能为空")
    }
    
    $.ajax({
        type: "POST",
        url: '/home/Ajax/pushCommit.html',
        data: {
            con_id:con_id,
            con_type:con_type,
            title: title,
            content: content
        },
        success: function(data){
            if(data['status'] == true){
                art.dialog({
                    icon: 'succeed',
                    content: data['msg'],
                    time: 2
                });
            }else {
                art.dialog({
                    icon: 'error',
                    content: data['msg'],
                    time: 2
                });
            }
            
        } 
    });
}

//添加收藏
function favorite(con_id, con_type, title, jurl){   
    $.ajax({
        type: "POST",
        url: '/home/Ajax/addFavorite.html',
        data: {
            con_id:con_id,
            con_type:con_type,
            title: title,
            jurl: jurl
        },
        success: function(data){
            if(data['status'] == true){
                art.dialog({
                    icon: 'succeed',
                    content: data['msg'],
                    time: 2
                });
            }else {
                art.dialog({
                    icon: 'error',
                    content: data['msg'],
                    time: 2
                });
            }
            
        } 
    });
}

function delFav(ids){
    var length = 0;
    if(ids) {
        length = 1;    		
    }else {
        ids    = getChecked();
        length = ids.length;
        ids    = ids.toString();
    }
    if(ids=='') {
        alert('请先选择一条信息');
        return ;
    }
    $.ajax({
        type: "POST",
        url: '/user/favorite/delFav.html',
        data: {
            id:ids
        },
        success: function(data){
            if(data['status'] == true){
                if( length == 1){
                    $('#fav_'+ids).fadeOut();
                }else{
                    ids = ids.split(',');
                    for(i = 0; i < ids.length; i++) {
                        $('#fav_'+ids[i]).fadeOut();
                    }
                }
                
                art.dialog({
                    icon: 'succeed',
                    content: data['msg'],
                    time: 2
                });
            }else {
                art.dialog({
                    icon: 'error',
                    content: data['msg'],
                    time: 2
                });
            }  
        } 
    });
}

